package com.skp.shaphan.connect;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import oracle.jdbc.pool.OracleDataSource;

import com.skp.job.ProjectJobException;
import com.skp.shaphan.savedConnections.SavedConnection;

public class Oracle extends BasicConnect {

	public Oracle(SavedConnection conn) {
		super(conn);
	}

	@Override
	public Connection connect(Properties props) {
		//Properties props = super.getConnectionDescription().getProperties();
		Connection connection = null;
		
		// http://www.oracle.com/technology/tech/java/sqlj_jdbc/htdocs/jdbc_faq.html#04_01
		// http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/111070_readme.html
		// http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc_111060.html
		OracleDataSource ods;
		try {
			ods = new OracleDataSource();
		} catch (SQLException e) {
			(new ProjectJobException(e)).displayException();
			return null;
		}
		ods.setDriverType(props.getProperty("connectionType"));
		ods.setUser(props.getProperty("userName"));
		ods.setPassword(props.getProperty("password"));
		String databaseName = props.getProperty("databaseName");
		if(databaseName.trim().length() > 0) {
			ods.setDatabaseName(databaseName);
		}
		ods.setNetworkProtocol("tcp");
		String serverName = props.getProperty("serverName");
		if(serverName.trim().length() > 0) {
			ods.setServerName(serverName);
		}
		String serviceName = props.getProperty("serviceName");
		if(serviceName.trim().length() > 0) {
			ods.setServiceName(serviceName);
		}
		String port = props.getProperty("portNumber");
		if(port.trim().length() > 0) {
			ods.setPortNumber(Integer.parseInt(port));
		}
		try {
			connection = ods.getConnection();
		} catch (SQLException e) {
			ProjectJobException.displayException(e);
		} catch (UnsatisfiedLinkError e) {
			(new ProjectJobException("Make sure the appropriate drivers are installed for Oracle.", e)).displayException();
		}
		return connection;
	}
}
